﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using CharacterCreation.Business.ViewModels;
using CharacterCreation.Data.Models;

namespace CharacterCreation.Business.Repositories
{
    public static class StatisticRepository
    {
        public static string GetStatisticValue(string statistic, int level)
        {
            string query = string.Format("Level = {0}", level);
            switch (statistic)
            {
                case "Strength":
                    VMStrength strength = new StrengthRepository().Get(query).FirstOrDefault();
                    if (strength == null) return "invalid query";
                    return string.Format("DA={0}; WA={1}; ISA={2}", 
                        strength.DamageAdjustment, 
                        strength.WeightAllowance, 
                        strength.InternalStrengthAdjustment);
                case "Dexterity":
                    VMDexterity dexterity = new DexterityRepository().Get(query).FirstOrDefault();
                    if (dexterity == null) return "invalid query";
                    return string.Format("IM={0}; CP={1}",
                        dexterity.InitiativeModifier,
                        dexterity.CombatPenalty);
                case "Fighting":
                    VMFighting fighting = new FightingRepository().Get(query).FirstOrDefault();
                    if (fighting == null) return "invalid query";
                    return string.Format("WP={0}; CP={1}",
                        fighting.WeaponPenalty,
                        fighting.CombatPenalty);
                case "Constitution":
                    VMConstitution constitution = new ConstitutionRepository().Get(query).FirstOrDefault();
                    if (constitution == null) return "invalid query";
                    return string.Format("BHPA={0}; HR={1}; RTP={2}; RTD={3}",
                        constitution.BeginningHitpointAdjustment,
                        constitution.HealingRate,
                        constitution.ResistanceToPoison,
                        constitution.ResistanceToDisease);
                case "Perception":
                    VMPerception perception = new PerceptionRepository().Get(query).FirstOrDefault();
                    if (perception == null) return "invalid query";
                    return string.Format("RA={0}",
                        perception.RangeAdjustment);
                case "Intelligence":
                    VMIntelligence intelligence = new IntelligenceRepository().Get(query).FirstOrDefault();
                    if (intelligence == null) return "invalid query";
                    return string.Format("XPA={0}; SA={1}",
                        intelligence.ExperienceAdjustment,
                        intelligence.SkillAdjustment);
                case "Will power":
                    VMWillPower willpower = new WillPowerRepository().Get(query).FirstOrDefault();
                    if (willpower == null) return "invalid query";
                    return string.Format("BEA={0}; EHR={1}",
                        willpower.BeginningEssenceAdjustment,
                        willpower.EssenceHealingRate);
                case "Charisma":
                    VMCharisma charisma = new CharismaRepository().Get(query).FirstOrDefault();
                    if (charisma == null) return "invalid query";
                    return string.Format("AA={0};",
                        charisma.AppearanceAdjustment);
                default:
                    return "";
            }
        }
    }

    public class StrengthRepository : Repository<VMStrength, Strength>
    {
        public void Initialize() {
            List<VMStrength> strengths = new List<VMStrength>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Strength.xml"));
            foreach (XElement element in xmldoc.Elements().Elements()){
                VMStrength strength = new VMStrength();
                strength.Id = Guid.NewGuid();
                strength.Level = int.Parse(element.Element("Level").Value);
                strength.DamageAdjustment = int.Parse(element.Element("DamageAdjustment").Value);
                strength.InternalStrengthAdjustment = int.Parse(element.Element("InternalStrengthAdjustment").Value);
                strength.WeightAllowance = int.Parse(element.Element("WeightAllowance").Value);

                strengths.Add(strength);
            }
            this.Add(strengths);
        }
    }

    public class DexterityRepository : Repository<VMDexterity, Dexterity>
    {
        public void Initialize()
        {
            List<VMDexterity> dexs = new List<VMDexterity>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Dexterity.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMDexterity dex = new VMDexterity();
                dex.Id = Guid.NewGuid();
                dex.Level = int.Parse(element.Element("Level").Value);
                dex.InitiativeModifier = int.Parse(element.Element("InitiativeModifier").Value);
                dex.CombatPenalty = int.Parse(element.Element("CombatPenalty").Value);

                dexs.Add(dex);
            }
            this.Add(dexs);
        }
    }

    public class FightingRepository : Repository<VMFighting, Fighting>
    {
        public void Initialize()
        {
            List<VMFighting> stats = new List<VMFighting>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Fighting.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMFighting stat = new VMFighting();
                stat.Id = Guid.NewGuid();
                stat.Level = int.Parse(element.Element("Level").Value);
                stat.WeaponPenalty = int.Parse(element.Element("WeaponPenalty").Value);
                stat.CombatPenalty = int.Parse(element.Element("CombatPenalty").Value);

                stats.Add(stat);
            }
            this.Add(stats);
        }
    }

    public class ConstitutionRepository : Repository<VMConstitution, Constitution>
    {
        public void Initialize()
        {
            List<VMConstitution> stats = new List<VMConstitution>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Constitution.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMConstitution stat = new VMConstitution();
                stat.Id = Guid.NewGuid();
                stat.Level = int.Parse(element.Element("Level").Value);
                stat.BeginningHitpointAdjustment = int.Parse(element.Element("BeginningHitpointAdjustment").Value);
                stat.HealingRate = double.Parse(element.Element("HealingRate").Value.Replace('.', ','));
                stat.ResistanceToDisease = int.Parse(element.Element("ResistanceToDisease").Value);
                stat.ResistanceToPoison = int.Parse(element.Element("ResistanceToPoison").Value);

                stats.Add(stat);
            }
            this.Add(stats);
        }
    }

    public class PerceptionRepository : Repository<VMPerception, Perception>
    {
        public void Initialize()
        {
            List<VMPerception> stats = new List<VMPerception>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Perception.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMPerception stat = new VMPerception();
                stat.Id = Guid.NewGuid();
                stat.Level = int.Parse(element.Element("Level").Value);
                stat.RangeAdjustment = int.Parse(element.Element("RangeAdjustments").Value);

                stats.Add(stat);
            }
            this.Add(stats);
        }
    }

    public class IntelligenceRepository : Repository<VMIntelligence, Intelligence>
    {
        public void Initialize()
        {
            List<VMIntelligence> stats = new List<VMIntelligence>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Intelligence.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMIntelligence stat = new VMIntelligence();
                stat.Id = Guid.NewGuid();
                stat.Level = int.Parse(element.Element("Level").Value);
                stat.ExperienceAdjustment = int.Parse(element.Element("ExperienceAdjustment").Value);
                stat.SkillAdjustment = int.Parse(element.Element("SkillAdjustment").Value);

                stats.Add(stat);
            }
            this.Add(stats);
        }
    }

    public class WillPowerRepository : Repository<VMWillPower, WillPower>
    {
        public void Initialize()
        {
            List<VMWillPower> stats = new List<VMWillPower>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/WillPower.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMWillPower stat = new VMWillPower();
                stat.Id = Guid.NewGuid();
                stat.Level = int.Parse(element.Element("Level").Value);
                stat.BeginningEssenceAdjustment = int.Parse(element.Element("BeginningEssenceAdjustment").Value);
                stat.EssenceHealingRate = double.Parse(element.Element("EssenceHealingRate").Value);

                stats.Add(stat);
            }
            this.Add(stats);
        }
    }

    public class CharismaRepository : Repository<VMCharisma, Charisma>
    {
        public void Initialize()
        {
            List<VMCharisma> stats = new List<VMCharisma>();
            XDocument xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/Content/Data/Charisma.xml"));
            foreach (XElement element in xmldoc.Elements().Elements())
            {
                VMCharisma stat = new VMCharisma();
                stat.Id = Guid.NewGuid();
                stat.Level = int.Parse(element.Element("Level").Value);
                stat.AppearanceAdjustment = int.Parse(element.Element("AppearanceAdjustment").Value);

                stats.Add(stat);
            }
            this.Add(stats);
        }
    }



}
